<?php

class Domain51_Service_Delicious_Posts
{
    private $_connection = null;
    
    public function __construct(Domain51_Util_Service_Connection $connection) {
        $this->_connection = $connection;
        Solar::loadClass('Domain51_Service_Delicious_Posts_Container');
    }

    public function get($tag = '', $dt = '', $url = '') {
        $params = array();
        if (!empty($tag)) {
            $params[] = 'tag=' . urlencode($tag);
        }
        
        if (!empty($dt)) {
            $params[] = 'dt=' . urlencode($dt);
        }
        
        if (!empty($url)) {
            $params[] = 'url=' . urlencode($url);
        }
            
        $uri = 'posts/get?' . implode('&', $params);
        return new Domain51_Service_Delicious_Posts_Container($this->_connection->doRequest($uri));
    }
    
    public function recent($tag = '', $count = 100) {
        $params = array();
        
        if (!empty($tag)) {
            $params[] = 'tag=' . urlencode($tag);
        }
        
        if (!empty($count)) {
            $params[] = 'count=' . (int)$count;
        }
        
        $uri = 'posts/recent?' . implode('&', $params);
        return new Domain51_Service_Delicious_Posts_Container($this->_connection->doRequest($uri));
    }
    
    public function all($tag = '') {
        $params = !empty($tag) ? 'tag=' . $tag : '';
        $uri = 'posts/all?' . $params;
        return new Domain51_Service_Delicious_Posts_Container($this->_connection->doRequest($uri));
    }
    
    public function dates($tag = '') {
        $params = !empty($tag) ? 'tag=' . $tag : '';
        $uri = 'posts/dates?' . $params;
        return new Domain51_Service_Delicious_Posts_Container($this->_connection->doRequest($uri), 'date');
    }
    
    public function add($url, $description, $extended = '', $tags = '', $dt ='', $replace = '', $shared = '') {
        $params = array();
        $params[] = 'url=' . urlencode($url);
        $params[] = 'description=' . urlencode($description);
        if (!empty($extended)) {
            $params[] = 'extended=' . urlencode($extended);
        }
        if (!empty($tags)) {
            $params[] = 'tags=' . urlencode($tags);
        }
        if (!empty($dt)) {
            $params[] = 'dt=' . urlencode($dt);
        }
        if (!empty($replace) && $replace == 'no') {
            $params[] = 'replace=no';
        }
        if (!empty($shared) && $shared == 'no') {
            $params[] = 'shared=no';
        }
        
        $uri = 'posts/add?' . implode('&', $params);
        $responseXml = $this->_connection->doRequest($uri);
        return ((string)$responseXml['code'] == 'done');
    }
    
    public function delete($url) {
        $uri = 'posts/delete?url=' . urlencode($url);
        $responseXml = $this->_connection->doRequest($uri);
        return ((string)$responseXml['code'] == 'done');
    }
    
    public function update() {
        $uri = 'posts/update';
        $responseXml = $this->_connection->doRequest($uri);
        return (string)$responseXml['time'];
    }
}

?>